package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielLibraryListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.MarerielLibraryBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MaterielDeleteLibraryActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    private MaterielLibraryListAdapter materielListAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materiel_delete_library;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setVisibility(View.GONE);

        initRecyclerView(rvMaterielList, this);
        materielListAdapter = new MaterielLibraryListAdapter(null);
        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(materielListAdapter);

        initEdit(etLot, s -> getMarerielLibraryInfo());
    }

    private void getMarerielLibraryInfo() {
        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<MarerielLibraryBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MarerielLibraryBean>>
                        (new TypeToken<BaseResponseBean<MarerielLibraryBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<MarerielLibraryBean>> response) {
                        super.onSuccess(response);
                        etLot.setText("");
                        etLot.requestFocus();
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            MarerielLibraryBean body = response.body().getResponse().getBody();
                            for (MarerielLibraryBean.MlotlistBean mlotlistBean : body.getMlotlist()) {
                                materielListAdapter.addData(0, mlotlistBean);
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<MarerielLibraryBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "IF.MLOTRETUREERP");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("MLOTID", getText(etLot));
                NetUtils.requestPostNet("IF.MLOTRETUREERP", "Common", MaterielDeleteLibraryActivity.this, map, callBack);

            }
        });

    }

    @Override
    protected void initData() {

    }

    public void showSubmitDialog(onSubmitListener listener) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要提交?", () -> {
            //确定
            if (listener != null) {
                listener.setOnSubmitListener();
            }
        }, () -> {
        }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");
    }

}

